<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="/static/libs/ckeditor/skins/bootstrapck/sample/js/jquery-1.11.0.min.js"></script>
    <script src="jsencrypt.js"></script>
    <script>

        var API = {
            BaseUrl: "http://timewoods.com/",
            user: "user/index/info",
            index: "index/index/index",
            privatekey: "index/index/privatekey",
            publickey: "index/index/publickey",
            encrypt: "index/index/encrypt",
            decrypt: "index/index/decrypt",

        };

        var Http = {
            asyncFlag: true,
            dataType: 'json',
            get: function (url, parm, beforeSend, success, error) {
                this.GetAjaxData(url, "get", parm, beforeSend, success, error);
            },
            post: function (url, parm, beforeSend, success, error) {
                this.GetAjaxData(url, "post", parm, beforeSend, success, error);
            },
            put: function (url, parm, beforeSend, success, error) {
                this.GetAjaxData(url, "put", parm, beforeSend, success, error);
            },
            delete: function (url, parm, beforeSend, success, error) {
                this.GetAjaxData(url, "delete", parm, beforeSend, success, error);
            },
            GetAjaxData: function (url, method, parm, beforeSend, success, error) {
                if (parm.encrypt !== undefined && parm.encrypt == true) {
                    var encrypt = new JSEncrypt();
                    encrypt.setPublicKey(PUBLIC);
                    encrypt.setPrivateKey(PRIVATE);
                    parm = {
                        encrypt: true,
                        encrypt_data: encrypt.encrypt(parm)
                    };

                }
                var headers = {};
                if (this.token !== undefined) {
                    headers = {"Token": Cache.get('token')}
                }
                $.ajax({
                    headers: headers,
                    type: method,
                    url: API.BaseUrl + url,
                    cache: false,
                    async: this.asyncFlag,
                    dataType: this.dataType,
                    data: parm,
                    beforeSend: function () {
                        if (typeof beforeSend === "function") {
                            beforeSend();
                        }
                    },
                    success: function (result) {
                        if (typeof success === "function") {
                            if (parm.encrypt !== undefined && parm.encrypt == true) {
                                console.log('b', result);
                                var decrypt = new JSEncrypt();
                                decrypt.setPublicKey(PUBLIC);
                                decrypt.setPrivateKey(PRIVATE);
                                result = decrypt.decrypt(result);
                                console.log('f', result);
                            }
                            success(result);
                        }
                    },
                    error: function (result) {
                        if (typeof error === "function") {
                            if (parm.encrypt !== undefined && parm.encrypt == true) {
                                var decrypt = new JSEncrypt();
                                decrypt.setPublicKey(PUBLIC);
                                decrypt.setPrivateKey(PRIVATE);
                                result = decrypt.decrypt(result);
                            }
                            error(result);
                        }
                    },
                });
            },
        };
    </script>
</head>
<body>

<script>


    var encrypt = new JSEncrypt();
    var PUBLIC = null, PRIVATE = null;
    Http.asyncFlag = false;
    Http.get(API.privatekey, '', '', function (data) {

        PRIVATE = data;
    });
    Http.get(API.publickey, '', '', function (data) {

        PUBLIC = data;
    });

    Http.asyncFlag = true;

    Http.post(API.encrypt, {encrypt: true}, '', function (data) {
        console.log('encrypt-加密返回', data);
    });
    Http.post(API.decrypt, {encrypt: true, username: 'Dragonlhp', passwd: 'llhp123123123'}, '', function (data) {
        console.log('decrypt-解密返回', data);
    });
</script>
<script>
    var encrypt = new JSEncrypt();
    var test = '此段内容用于加密数据测试文本';
    encrypt.setPublicKey(PUBLIC);

    var encode = encrypt.encrypt(test);

    console.log('加密前的文字：' + test);
    console.log('加密后的密文' + encode);

    encrypt.setPrivateKey(PRIVATE);

    var decode = encrypt.decrypt(encode);
    console.log('解密后的文字：' + decode);
</script>
</body>
</html>